package uk.ac.glasgow.oose2.gcf.view.gui;
/**
 * Graphical view for Extended calculator - shows how to extend GuiBasic
 *  
 * @author J Sventek
 * @author tws
 * @version 1.0
 * 
 * Edit history:
 * 08/03/2008 - initial release
 */

import java.awt.GridLayout;
import java.awt.Color;

import javax.swing.JPanel;

import uk.ac.glasgow.oose2.gcf.model.ScientificCalculator;

public class GuiScientific extends GuiBasic {
	private static final long serialVersionUID = -5368279260811158496L;
	
	/** The calculator instance for this view, accessible via the ScientificCalculator features.*/
	private ScientificCalculator sciCalc;
	
	/**
	 * Constructs a new extended graphical view for the specified calculator model.
	 * @param theCalc the model for the view to present.
	 */ 
	public GuiScientific(ScientificCalculator theCalc) {
		//Pass the calculator to the super class (via the restricted BasicCalculator interface)
		super(theCalc);
		//Retain the extended calculator instance to access extended features.
		this.sciCalc = theCalc;
	}
	
	/**
	 * Overrides super.initialiseUI() to provide extended features.
	 */
	public void initialiseUI() {
		// Create panel container for extra buttons
		JPanel jplSci = new JPanel();			
		
		//  set layout manager for extra buttons
		jplSci.setLayout(new GridLayout(2, 4, 2, 2));
	
		// Add extra buttons - rad, sin, cos, tan
		jplSci.add(new CalculatorButton("rad", Color.orange));
		jplSci.add(new CalculatorButton("sin", Color.orange));
		jplSci.add(new CalculatorButton("cos", Color.orange));
		jplSci.add(new CalculatorButton("tan", Color.orange));
		
		// Add extra buttons - e, log, exp, pow
		jplSci.add(new CalculatorButton("e", Color.green));
		jplSci.add(new CalculatorButton("log", Color.red));
		jplSci.add(new CalculatorButton("exp", Color.red));
		jplSci.add(new CalculatorButton("pow", Color.red));

		//provide that JPanel object to the super class
		super.setExtra(jplSci);
		
		// init the super class UI components
		super.initialiseUI();
		
	}

	/**
	 * overrides processOperation() in super class to handle Extended-specific operations.
	 * if not an Extended-specific operation, defer to super.processOperation to handle
	 * @param cmd command string for calculator operation
	 */
	public void processOperation(String cmd) {
		try {
			if (cmd.equalsIgnoreCase("e"))
				sciCalc.e();
			else if (cmd.equalsIgnoreCase("log"))
				sciCalc.ln();
			else if (cmd.equalsIgnoreCase("exp"))
				sciCalc.exp();
			else if (cmd.equalsIgnoreCase("pow"))
				sciCalc.pow();
			else if (cmd.equalsIgnoreCase("rad"))
				sciCalc.rad();
			else if (cmd.equalsIgnoreCase("sin"))
				sciCalc.sin();
			else if (cmd.equalsIgnoreCase("cos"))
				sciCalc.cos();
			else if (cmd.equalsIgnoreCase("tan"))
				sciCalc.tan();
			else super.processOperation(cmd);
			
			displayTop();
		
		} catch (Exception ex) {
			displayError(ex.toString());
		}
		
	}
}
